package com.roc.service.impl.diaodanImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.dao.IorgDao;
import com.roc.dao.diaodan.IDiaoDanDao;
import com.roc.dao.setting.ISettingFlagDao;
import com.roc.pojo.AuditingItem;
import com.roc.pojo.BlackList;
import com.roc.pojo.CalcuBean;
import com.roc.pojo.DiaoDanBean;
import com.roc.pojo.DuizhangBean;
import com.roc.pojo.SettFlagBean;
import com.roc.pojo.SettingBean;
import com.roc.pojo.SettingItemBean;
import com.roc.service.ServiceException;
import com.roc.service.diaodan.IDiaodanService;
import com.roc.util.Constance;
import com.roc.util.constant.FrozenStatus;
import com.roc.util.constant.SysLogType;
import com.roc.web.action.diaodan.DiaoDanApplySearchCondition;

@Service
@Transactional
public class DiaodanServiceImpl implements IDiaodanService {
	private Logger logger = Logger.getLogger(DiaodanServiceImpl.class);
	@Autowired
	private IorgDao orgDao;
	@Autowired
	ISQLBaseDao sqlBaseDao;
	@Autowired
	private IBaseDao baseDao;
	@Autowired
	private IDiaoDanDao diaodanDao;


	
// 查询出款信息
	@Override
	public List<DiaoDanBean>  querySettingItemList(DiaoDanApplySearchCondition diaodan) throws ServiceException {
		List<DiaoDanBean>  diaoDanlist =new ArrayList<DiaoDanBean>();

		try {
			
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql = "  select es.belong_branch,em.agent_id, es.mid,es.mer_name,e.etsdate,e.etstime, e.termid, e.termtrace, e.etstrace, e.amount, "+
				"  e.etstrancode,e.respcode,es.comm_mode,es.comm_rate,es.comm_amt,es.mcc,e.period,es.acc_no,e.account,e.merfee,e.calcuamount,t.reversal from t_transjnls_calcu e, esmidinfo es, esmidinfo_merinfo em,t_trans_register t where es.mid = e.merno and em.merchant = es.mid and e.etstrancode='020000' and e.respcode='00' and e.reversal='0'  and e.etstrace = t.etstrace(+) ";
      
           	if(StringUtils.isNotBlank(diaodan.getMerNo())){
           		sql = sql+" and es.mid = '"+diaodan.getMerNo().trim()+"'";
           	}  
           	if(StringUtils.isNotBlank(diaodan.getTermtrace())){
           		sql = sql+"  and e.termtrace = '"+diaodan.getTermtrace().trim()+"'";
           	}	
         	if(StringUtils.isNotBlank(diaodan.getEtstermId())){
         		sql = sql+"   and e.termid = '"+diaodan.getEtstermId().trim()+"'";
         	}
         	if(StringUtils.isNotBlank(diaodan.getEtsdate())){
         		sql = sql+"    and e.etsdate = '"+diaodan.getEtsdate().trim()+"'";
         	}
         	if(StringUtils.isNotBlank(diaodan.getPeriod())){
         		sql = sql+"    and e.period= '"+diaodan.getPeriod().trim()+"'";
         	}
         	if(StringUtils.isNotBlank(diaodan.getAccountno())){
         		sql = sql+"    and e.account= '"+diaodan.getAccountno().trim()+"'";
         	}
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				
				Iterator iterator = objList.iterator();
				while (iterator.hasNext()) {
					Object[] obj = (Object[]) iterator.next();
					DiaoDanBean diaoDanBean=new DiaoDanBean();
					if (obj[0] != null && !"".equals(obj[0])) {
						diaoDanBean.setOrgcode(obj[0].toString());
					}
					if (obj[1] != null && !"".equals(obj[1])) {
						diaoDanBean.setAgentid(obj[1].toString());
					}
					if (obj[2] != null && !"".equals(obj[2])) {
						diaoDanBean.setMerchantno(obj[2].toString());
					}
					if (obj[3] != null && !"".equals(obj[3])) {
						diaoDanBean.setMerchantname(obj[3].toString());
					}
					if (obj[4] != null && !"".equals(obj[4])) {
						diaoDanBean.setTransdate(obj[4].toString());
					}
					if (obj[5] != null && !"".equals(obj[5])) {
						diaoDanBean.setTranstime(obj[5].toString());
					}
					if (obj[6] != null && !"".equals(obj[6])) {
						diaoDanBean.setTermid(obj[6].toString());
					}
					 if (obj[7] != null && !"".equals(obj[7])) {
						 diaoDanBean.setTermtrace(obj[7].toString());
					 }
					if (obj[8] != null && !"".equals(obj[8])) {
						diaoDanBean.setEtstrace(obj[8].toString());
					}
					 if (obj[9] != null && !"".equals(obj[9])) {
						 Double dou = (double) Math.round(Double.parseDouble(String
									.valueOf(obj[9].toString())) * 100 * 100) / 100;
						 long tr =  Math.round(dou);
						 diaoDanBean.setTransamt(tr);
										}
					if (obj[10] != null && !"".equals(obj[10])) {
						diaoDanBean.setEtstrancode(obj[10].toString());
					}
					if (obj[11] != null && !"".equals(obj[11])) {
						diaoDanBean.setRespcode(obj[11].toString());
					}
					if (obj[12] != null && !"".equals(obj[12])) {
						diaoDanBean.setCommmode(obj[12].toString());
					}
					if (obj[13] != null && !"".equals(obj[13])) {
						diaoDanBean.setCommrate(obj[13].toString());
					}
					if (obj[14] != null && !"".equals(obj[14])) {
						diaoDanBean.setCommamt(obj[14].toString());
					}
					if (obj[15] != null && !"".equals(obj[15])) {
						diaoDanBean.setMcc(obj[15].toString());
					}
					if (obj[16] != null && !"".equals(obj[16])) {
						diaoDanBean.setPeriod(obj[16].toString());
					}
					if (obj[17] != null && !"".equals(obj[17])) {
						diaoDanBean.setAcctno(obj[17].toString());
					}
					if (obj[18] != null && !"".equals(obj[18])) {
						diaoDanBean.setAccontno(obj[18].toString());
					}
					if (obj[19] != null && !"".equals(obj[19])) {
						
						 Double d = (double) Math.round(Double.parseDouble(String
									.valueOf(obj[19].toString())) * 100 * 100) / 100;
						 long fr =  Math.round(d);
						diaoDanBean.setMerfee(fr);
					}
					if (obj[20] != null && !"".equals(obj[20])) {
						 Double d = (double) Math.round(Double.parseDouble(String
									.valueOf(obj[20].toString())) * 100 * 100) / 100;
						 long fr =  Math.round(d);
						diaoDanBean.setCacluamount(fr);
					}
					if (obj[21] != null && !"".equals(obj[21])) {
						diaoDanBean.setReversal(obj[21].toString());
					}else{
						diaoDanBean.setReversal("99");
					}
					
					
					diaoDanlist.add(diaoDanBean);
				}
				
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询调单信息失败~！");
		}
		return diaoDanlist;
	}
	
	// 查询调单信息
	@Override
	public DiaoDanBean queryDiaoDanBean(String termtrace,String acountno) throws ServiceException {
		DiaoDanBean  diaoDanBean=new DiaoDanBean();

		try {
			if(StringUtils.isNotBlank(termtrace)){
			// settingflagList=settingFlagDao.querySettingFlagList();
			String sql = "  select es.belong_branch,em.agent_id, es.mid,es.mer_name,e.etsdate,e.etstime, e.termid, e.termtrace, e.etstrace, e.amount, "+
				"  e.etstrancode,e.respcode,es.comm_mode,es.comm_rate,es.comm_amt,es.mcc,e.period,es.acc_no,e.account,e.merfee,e.calcuamount from t_transjnls_calcu e, esmidinfo es, esmidinfo_merinfo em where es.mid = e.merno and em.merchant = es.mid and e.etstrancode='020000' and e.respcode='00' and e.reversal='0' ";
      
           
           	if(StringUtils.isNotBlank(termtrace)){
           		sql = sql+"  and e.termtrace = '"+termtrace.trim()+"'";
           	}	
         	if(StringUtils.isNotBlank(acountno)){
         		sql = sql+"   and e.account = '"+acountno.trim()+"'";
         	}
         	
         	
			List<Object> objList = new ArrayList<Object>();
			objList = sqlBaseDao.queryAll(sql);
			if (objList != null && objList.size() > 0) {
				
				
					Object[] obj = (Object[]) objList.get(0);
					
					if (obj[0] != null && !"".equals(obj[0])) {
						diaoDanBean.setOrgcode(obj[0].toString());
					}
					if (obj[1] != null && !"".equals(obj[1])) {
						diaoDanBean.setAgentid(obj[1].toString());
					}
					if (obj[2] != null && !"".equals(obj[2])) {
						diaoDanBean.setMerchantno(obj[2].toString());
					}
					if (obj[3] != null && !"".equals(obj[3])) {
						diaoDanBean.setMerchantname(obj[3].toString());
					}
					if (obj[4] != null && !"".equals(obj[4])) {
						diaoDanBean.setTransdate(obj[4].toString());
					}
					if (obj[5] != null && !"".equals(obj[5])) {
						diaoDanBean.setTranstime(obj[5].toString());
					}
					if (obj[6] != null && !"".equals(obj[6])) {
						diaoDanBean.setTermid(obj[6].toString());
					}
					 if (obj[7] != null && !"".equals(obj[7])) {
						 diaoDanBean.setTermtrace(obj[7].toString());
					 }
					if (obj[8] != null && !"".equals(obj[8])) {
						diaoDanBean.setEtstrace(obj[8].toString());
					}
					 if (obj[9] != null && !"".equals(obj[9])) {
						 Double d = (double) Math.round(Double.parseDouble(String
									.valueOf(obj[9].toString())) * 100 * 100) / 100;
						// long fr =  Math.round(d);
						 diaoDanBean.setTransamt(d);
					}
					if (obj[10] != null && !"".equals(obj[10])) {
						diaoDanBean.setEtstrancode(obj[10].toString());
					}
					if (obj[11] != null && !"".equals(obj[11])) {
						diaoDanBean.setRespcode(obj[11].toString());
					}
					if (obj[12] != null && !"".equals(obj[12])) {
						diaoDanBean.setCommmode(obj[12].toString());
					}
					if (obj[13] != null && !"".equals(obj[13])) {
						diaoDanBean.setCommrate(obj[13].toString());
					}
					if (obj[14] != null && !"".equals(obj[14])) {
						diaoDanBean.setCommamt(obj[14].toString());
					}
					if (obj[15] != null && !"".equals(obj[15])) {
						diaoDanBean.setMcc(obj[15].toString());
					}
					if (obj[16] != null && !"".equals(obj[16])) {
						diaoDanBean.setPeriod(obj[16].toString());
					}
					if (obj[17] != null && !"".equals(obj[17])) {
						diaoDanBean.setAcctno(obj[17].toString());
					}
					if (obj[18] != null && !"".equals(obj[18])) {
						diaoDanBean.setAccontno(obj[18].toString());
					}
					if (obj[19] != null && !"".equals(obj[19])) {
						Double d = (double) Math.round(Double.parseDouble(String
								.valueOf(obj[19].toString())) * 100 * 100) / 100;
					 long fr =  Math.round(d);
						diaoDanBean.setMerfee(fr);
					}
					if (obj[20] != null && !"".equals(obj[20])) {
						Double d = (double) Math.round(Double.parseDouble(String
								.valueOf(obj[20].toString())) * 100 * 100) / 100;
					 long fr =  Math.round(d);
						diaoDanBean.setCacluamount(fr);
					}
				}
				
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage());

			throw new ServiceException("查询调单信息失败~！");
		}
		return diaoDanBean;
	}
	
	// 查询出款信息
		@Override
		public List<SettingBean> querySettinglist(String  merno) throws ServiceException {
			List<SettingBean> list =  new ArrayList<SettingBean>();

			try {
				list = diaodanDao.querySettinglist(merno);
			} catch (DaoException e) {
				logger.debug(e.getMessage());
				throw new ServiceException("查询结算失败~！");
			}
			return list;
		}
		@Override
		public boolean diaodanApply(AuditingItem auditing,DiaoDanBean diaodan,String stuts,List<String> list) throws ServiceException {
			boolean result = false;
			try {
				if(!"1".equals(stuts)){
					result = diaodanDao.diaodanApply(auditing, diaodan);
				}else{
					diaodanDao.updatediaodan(diaodan);
				}

					/** *****添加调单申请成功---记录日志****** */
					baseDao.addSyslog(SysLogType.DIAODAN_APPLY.getCode(),
							SysLogType.DIAODAN_APPLY.getName(), auditing
									.getOperators(), "操作员"
									+ auditing.getOperators() + "【"
									+ auditing.getAudit_name() + "】【"
									+ diaodan.getMerchantno() + "】成功");
					//冻结结算信息
					for(int i=0;i<list.size();i++){
						String settid = list.get(i).trim();
						if(!"".equals(settid)){
							diaodanDao.frozenSetting(settid);	
						}
					
					}

			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("调单申请失败~！");
			}
			return result;
		}
		@Override
		public DiaoDanBean queryDiaoDanBean(String id) throws ServiceException {

			DiaoDanBean sett = new DiaoDanBean();
			try {
				sett = diaodanDao.queryDiaoDanBean(id);
			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("查询结算信息失败~！");
			}

			return sett;
		}

		@Override
		public int queryblackcount(String merno) throws ServiceException {
			int i =0;
			try {
				i=diaodanDao.queryblackcount(merno);
			} catch (Exception e) {
				logger.debug(e.getMessage(), e);
				throw new ServiceException("更新失败！");
			}
			
			return i;
		}
		@Override
		public void insertblacklist(BlackList black) throws ServiceException {
			try {
					baseDao.editSave(black);
			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("调单申请失败~！");
			}
		}
		@Override
		public void insertsett(SettingBean sett) throws ServiceException {
			try {
					baseDao.editSave(sett);
			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("插入新的结算记录！");
			}
		}
		@Override
		public void updatesett(SettingBean sett) throws ServiceException {
			try {
					baseDao.editUpdate(sett);
			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("修改结算记录！");
			}
		}
		@Override
		public boolean diaodanHandle(AuditingItem auditing,DiaoDanBean diaodan) throws ServiceException {
			boolean result = false;
			try {
					diaodanDao.editSave(auditing);
					diaodanDao.updatediaodan(diaodan);
					/*******添加调单撤销---记录日志*******/
					baseDao.addSyslog(SysLogType.DIAODAN_HANDLE.getCode(),
							SysLogType.DIAODAN_HANDLE.getName(), auditing
									.getOperators(), "操作员"
									+ auditing.getOperators() + "【"
									+ auditing.getAudit_name() + "】【"
									+ diaodan.getMerchantno() + "】成功");
					//冻结结算信息
					String settid= diaodan.getSettleorderno();
					if(settid!=null&&!"".equals(settid)){
						String[] list = settid.split(",");
						for(int i=0;i<list.length;i++){
							String setid = list[i];
							if(!"".equals(setid)){
								diaodanDao.unfrozenSetting(setid);	
							}
						}	
					}
					
					result=true;
			} catch (DaoException e) {
				logger.debug(e.getMessage());

				throw new ServiceException("调单申请失败~！");
			}
			return result;
		}
		// 查询出款信息
		@Override
		public List<SettingBean> querySettlist(String  settid) throws ServiceException {
			List<SettingBean> list =  new ArrayList<SettingBean>();

			try {
				String[] list1= settid.split(",");
				for(int i=0;i<list1.length;i++){
					String setid = list1[i];
					SettingBean sett = new SettingBean();
					sett = diaodanDao.querySettlist(setid);
					list.add(sett);
				}
				
			} catch (DaoException e) {
				logger.debug(e.getMessage());
				throw new ServiceException("查询结算失败~！");
			}
			return list;
		}
}
